package com.anolesoft.yr.mbus.code.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import com.anolesoft.yr.mbus.code.entity.CodeItemDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * @author zhj
 * @date 2020/9/17 9:33
 */
@Mapper
public interface CodeItemMapper {
    @Insert("insert into code_item (" +
            "objuid, " +
            "fac_code," +
            "item_code_name, " +
            "item_type, " +
            "item_length, " +
            "trim_aspect, " +
            "item_format, " +
            "item_geneclass, " +
            "item_placeholder, " +
            "item_desc, " +
            "c_user_uid," +
            "c_time," +
            "m_user_uid," +
            "m_time," +
            "del_flag," +
            "remark" +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}, " +
            "#{factoryCode,jdbcType=VARCHAR}, " +
            "#{itemCodeName,jdbcType=VARCHAR}, " +
            "#{itemType,jdbcType=INTEGER}, " +
            "#{itemLength,jdbcType=INTEGER}, " +
            "#{trimAspect,jdbcType=INTEGER}, " +
            "#{itemFormat,jdbcType=VARCHAR}, " +
            "#{itemGeneClass,jdbcType=VARCHAR}, " +
            "#{itemPlaceholder,jdbcType=VARCHAR}, " +
            "#{itemDesc,jdbcType=VARCHAR}, " +
            "#{createUserUid,jdbcType=VARCHAR}, " +
            "now(), " +
            "#{modifyUserUid,jdbcType=VARCHAR}, " +
            "now(), " +
            "0," +
            "#{remark,jdbcType=VARCHAR} " +
            ")")
    int insert(CodeItemDO codeItemDO);

    @Update("update code_item set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "item_code_name = #{itemCodeName,jdbcType=VARCHAR}, " +
            "item_type = #{itemType,jdbcType=INTEGER}, " +
            "item_length = #{itemLength,jdbcType=INTEGER}, " +
            "trim_aspect = #{trimAspect,jdbcType=INTEGER}, " +
            "item_format = #{itemFormat,jdbcType=VARCHAR}, " +
            "item_geneclass = #{itemGeneClass,jdbcType=VARCHAR}, " +
            "item_placeholder = #{itemPlaceholder,jdbcType=VARCHAR}, " +
            "item_desc = #{itemDesc,jdbcType=VARCHAR}, " +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}, " +
            "m_time = now(), " +
            "remark = #{remark,jdbcType=VARCHAR} " +
            " where " +
            "objuid = #{objuid,jdbcType=VARCHAR}")
    int update(CodeItemDO codeItemDO);

    @Update("update code_item set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "CodeItemResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "item_code_name", property = "itemCodeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "item_type", property = "itemType", jdbcType = JdbcType.INTEGER),
            @Result(column = "item_length", property = "itemLength", jdbcType = JdbcType.INTEGER),
            @Result(column = "trim_aspect", property = "trimAspect", jdbcType = JdbcType.INTEGER),
            @Result(column = "item_format", property = "itemFormat", jdbcType = JdbcType.VARCHAR),
            @Result(column = "item_geneclass", property = "itemGeneClass", jdbcType = JdbcType.VARCHAR),
            @Result(column = "item_placeholder", property = "itemPlaceholder", jdbcType = JdbcType.VARCHAR),
            @Result(column = "item_desc", property = "itemDesc", jdbcType = JdbcType.VARCHAR),

            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),

            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR)
    })
    @Select("select " +
            "objuid, fac_code, item_code_name, item_type, item_length, trim_aspect, " +
            "item_format, item_geneclass, item_placeholder, item_desc, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark  " +
            "from code_item where del_flag = 0 ")
    List<CodeItemDO> selectAll();

    @ResultMap("CodeItemResultMap")
    @Select("select " +
            "objuid, fac_code, item_code_name, item_type, item_length, trim_aspect, " +
            "item_format, item_geneclass, item_placeholder, item_desc, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark  " +
            "from code_item where del_flag = 0 " +
            "and objuid = #{id,jdbcType=VARCHAR}")
    CodeItemDO selectOneById(@Param("id") String id);

    @ResultMap("CodeItemResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, item_code_name, item_type, item_length, trim_aspect, " +
            "item_format, item_geneclass, item_placeholder, item_desc, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark  " +
            "from code_item where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.itemCodeName !=null and o.itemCodeName != '' \">" +
            "   and item_code_name like #{o.itemCodeName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.itemType !=null \">" +
            "   and item_type = #{o.itemType, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<CodeItemDO> selectPageList(@Param("o") CodeItemDO codeItemDO, Page<CodeItemDO> page);

    @Select("select count(objuid) from code_item where del_flag = 0 and item_code_name = #{itemCodeName, jdbcType=VARCHAR}")
    Integer checkUniqueForItemCodeName(@Param("itemCodeName") String itemCodeName);

    @Select("select count(objuid) from code_item where del_flag = 0 and item_code_name = #{itemCodeName, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForItemCodeNameUpdate(@Param("itemCodeName") String itemCodeName, @Param("id") String id);
}
